package com.jcl.recruit.wechat.service;

import com.jcl.core.security.ShareUserToken;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.wechat.utils.WechatUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Service
public class WechatService {

    /**
     * @param currentUrl
     * @return
     */
    public WechatUtils.SignatureInfo getSignature(String currentUrl) {
        WechatUtils.SignatureInfo info = WechatUtils.SignatureInfo.initSignature(currentUrl,
                WechatUtils.getAccessToken(), WechatUtils.getTicket());
        return info;
    }


    public Map<String, Object> inviteUser() {

        String corpCode = RequestUtils.getCorpCode();
        String userId = RequestUtils.getUserId();
        Map<String, Object> map = new HashMap<>();
        String url_token = ShareUserToken.encodeInviteToken(corpCode, userId);
        String url = SystemConfig.getSystemWebUrl() + SystemConfig.getWechatLoginUrl();
        try {
            map.put("REDIRECT_URL", URLEncoder.encode(url, "UTF-8"));
            map.put("STATE", url_token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return map;
    }
}
